const invoices = require("../asset/invoices.json")
const plays = require("../asset/plays.json")


/**
 * 核心程序:拆分阶段（1.生成中转数据结构； 2.将数据渲染成文本或 HTML）
 * @param {*} invoice 
 * @param {*} plays 
 * @returns 
 */
function statement(invoice, plays) {
  // 用于中转的数据结构
  const statementData = {};
  // 顾客信息
  statementData.customer = invoice.customer;
  // 剧目信息：通过 map 函数遍历 performances，将其属性进行扩展
  statementData.performances = invoice.performances.map(enrichPerformance);
  // 总费用
  statementData.totalAmount = totalAmount(statementData);
  // 总积分
  statementData.totalVolumeCredits = totalVolumeCredits(statementData);
  return renderPlainText(statementData, plays);

  /**
   * 将其中每一个 aPerformance 的属性由【playID,audience】 扩展为【playID,audience，play(name,type)】
   * @returns 新增了额外属性的 Performance 对象
   */
  function enrichPerformance(aPerformance) {
    const result = Object.assign({}, aPerformance);
    result.play = playFor(result);
    result.amount = amountFor(result);
    result.volumeCredits = volumeCreditsFor(result);
    return result;
  }

  /**
   * 根据账单中的数据获取对应剧目信息
   * @param {*} aPerformance 
   * @returns 
   */
  function playFor(aPerformance) {
    return plays[aPerformance.playID];
  }

  /**
   * 计算单场剧目的费用
   * @param {*} aPerformance 
   * @returns 
   */
  function amountFor(aPerformance) {
    let result = 0;
    switch (aPerformance.play.type) {
      case "tragedy":
        result = 40000;
        if (aPerformance.audience > 30) {
          result += 1000 * (aPerformance.audience - 30);
        }
        break;
      case "comedy":
        result = 30000;
        if (aPerformance.audience > 20) {
          result += 10000 + 500 * (aPerformance.audience - 20);
        }
        result += 300 * aPerformance.audience;
        break;
      default:
        throw new Error(`unknown type: ${aPerformance.play.type}`);
    }
    return result;
  }
  /**
   * 计算单场剧目产生的观众量积分
   * @param {*} perf 
   * @returns 积分
   */
  function volumeCreditsFor(aPerformance) {
    let result = 0;
    result += Math.max(aPerformance.audience - 30, 0);
    if ("comedy" === aPerformance.play.type) result += Math.floor(aPerformance.audience / 5);
    return result;
  }
  /**
   * 计算总费用
   */
  function totalAmount(data) {
    return data.performances
      .reduce((total, p) => total + p.amount, 0);
  }

  /**
   * 计算观众量积分总和
   */
  function totalVolumeCredits(data) {
    return data.performances
      .reduce((total, p) => total + p.volumeCredits, 0);
  }

}

/**
 * 
 * @param {*} invoice 
 * @param {*} plays 
 * @returns 
 */
function renderPlainText(data, plays) {
  let result = `Statement for ${data.customer}\n`;
  for (let perf of data.performances) {
    result += ` ${perf.play.name}: ${usd(perf.amount)} (${perf.audience} seats)\n`;
  }
  result += `Amount owed is ${usd(data.totalAmount)}\n`;
  result += `You earned ${data.totalVolumeCredits} credits\n`;
  return result;



  /**
   * 将数字格式化为 货币 & 将美分转换为美元（除以100）
   * @param {*} aNumber 
   */
  function usd(aNumber) {
    return new Intl.NumberFormat("en-US",
      {
        style: "currency", currency: "USD",
        minimumFractionDigits: 2
      }).format(aNumber / 100);
  }





}




// 调用
let invoice = invoices[0];
res = statement(invoice, plays);
console.log(res);
